#include "connectthread.h"
#include <QFile>

ConnectThread::ConnectThread(QObject * parent,const int &socketId,const QString &version,const QList<QByteArray> &fileArray)
{
    nextBlockSize=0;
    clientSocket=new QTcpSocket(this);
    clientSocket->setSocketDescriptor(socketId);

    this->version=version;
    this->_fileArray=fileArray;

    connect(clientSocket, SIGNAL(disconnected()),this, SLOT(threadOver()));
    connect(clientSocket,SIGNAL(readyRead()),this,SLOT(communicate()));
    qDebug()<<"Update:connect to server successful,client id is"<<socketId;

    //发送当前服务器上的客户端版本信息给升级客户端
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_5);
    out<<quint64(0)<<quint16(0x0001)<<version;
    out.device()->seek(0);
    out<<quint64(block.size()-sizeof(quint16));
    clientSocket->write(block);
}
ConnectThread::~ConnectThread() {
}
/**
 * 和客户端通信
 */
void ConnectThread::communicate()
{
    //创建数据流
    QDataStream in(clientSocket);
    in.setVersion(QDataStream::Qt_4_5);
    while(1){
        if (nextBlockSize == 0){
            if (clientSocket->bytesAvailable() < sizeof(quint64)){
                break;
            }
            in >> nextBlockSize;
        }
        if ((quint64)(clientSocket->bytesAvailable())<nextBlockSize){
            break;;
        }
        quint16 protocol;
        in>>protocol;
        qDebug()<<"Update:protocol:"<<QString::number(protocol,16);
        if(protocol == 1){
            //传输xml文件
            QByteArray fblock;
            QDataStream fout(&fblock, QIODevice::WriteOnly);
            fout.setVersion(QDataStream::Qt_4_5);
            fout<<quint64(0)<<quint16(0x0002);

            QFile fileXml("update.xml");
            QByteArray file;
            if(fileXml.open(QIODevice::ReadOnly)){
                file = fileXml.readAll();
                fout << file;
            }
            fout.device()->seek(0);
            fout<<quint64(fblock.size()-sizeof(quint64));
            clientSocket->write(fblock);
            qDebug()<<"Update:size:"<<humanDisplay(fblock.size()-sizeof(quint64));
        }
        else if(protocol == 2){
            //升级文件
            QByteArray fblock;
            QDataStream fout(&fblock, QIODevice::WriteOnly);
            fout.setVersion(QDataStream::Qt_4_5);
            fout<<quint64(0)<<quint16(0x0003);
            foreach (QByteArray file, _fileArray) {
                fout << file;
            }
            fout.device()->seek(0);
            fout<<quint64(fblock.size()-sizeof(quint64));
            clientSocket->write(fblock);
            qDebug()<<"Update:size:"<<humanDisplay(fblock.size()-sizeof(quint64));
        }
        nextBlockSize=0;
    }
}

QString ConnectThread::humanDisplay(int bytes)
{
    double kb = bytes / 1024;
    if(kb < 1024){
        return QString::number(kb,'f',2) + QString("KB");
    }else{
        double mb = kb / 1024;
        if(mb < 1024){
            return QString::number(mb,'f',2) + QString("MB");
        }else{
            double gb = mb / 1024;
            return QString::number(gb,'f',2) + QString("GB");
        }
    }
}
/**
 * 释放网络连接资源
 */
void ConnectThread::threadOver()
{
    qDebug()<<"Update:a client connection closed...";
    clientSocket->deleteLater();
    this->deleteLater();
}
